import db, {thingsModelList} from '../../database/lowDB/connectDb.js';
import {unioformDateTransform,autoIncreamentKey} from '../../utils/index.js'

class thingsModelListModel {
    constructor() {
        this.tableName = 'thingsModelList';
    }

    // 根据用户id和角色id查询事项清单
    async getThingsModelListByUserIdAndRoleCode({userId, roleCode}) {   
        return thingsModelList.filter(model => model.userId === userId && model.roleCode === roleCode);
    }
    
    // 根据id查询事项清单
    async getThingModelById(id){
        return thingsModelList.find((item)=>item.id === id) 
    }



    // 新增事项清单 —— 单条新增
    async addThingsModelList({userInfo, thingsModelData}) {
        const {userId, roleCode} = userInfo;
        const {modelName, theme} = thingsModelData;
        const newModel = {
            id: autoIncreamentKey(thingsModelList), 
            userId,
            roleCode,
            modelName,
            theme,
            createDate: unioformDateTransform(new Date()),
        };
        thingsModelList.unshift(newModel);
        db.write();
        return true;
    }
}   

export default new thingsModelListModel();